﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using L2C;
using System.Text.RegularExpressions;
using System.Text;
using L2C.Models.Administrator;

namespace L2C.Controllers.Administrator
{
    [Authorize]
    public class PageContentController : Controller
    {
        private L2CEntities db = new L2CEntities();

        //
        // GET: /Page/

        public ViewResult Index()
        {
            return View(db.StaticContents.ToList());
        }

        //
        // GET: /Page/Details/5

        public ViewResult Details(int id)
        {
            StaticContent staticcontent = db.StaticContents.Single(s => s.ID == id);
            return View(staticcontent);
        }

        //
        // GET: /Page/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Page/Create

        [HttpPost]
        public ActionResult Create(PageContentModel pageContent)
        {
            if (ModelState.IsValid)
            {
                var content = pageContent.PopulateToEntity();
                db.StaticContents.AddObject(content);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(pageContent);
        }
        
        //
        // GET: /Page/Edit/5
 
        public ActionResult Edit(int id)
        {
            PageContentModel content = new PageContentModel();
            StaticContent staticContent = db.StaticContents.SingleOrDefault(s => s.ID == id);
            if (staticContent != null) content = PageContentModel.Parse(staticContent);
            return View(content);
        }

        //
        // POST: /Page/Edit/5

        [HttpPost]
        public ActionResult Edit(PageContentModel pageContent)
        {
            if (ModelState.IsValid)
            {
                var content = pageContent.PopulateToEntity();
                db.StaticContents.Attach(content);
                db.ObjectStateManager.ChangeObjectState(content, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(pageContent);
        }

        //
        // GET: /Page/Delete/5
 
        public ActionResult Delete(int id)
        {
            StaticContent staticcontent = db.StaticContents.Single(s => s.ID == id);
            return View(staticcontent);
        }

        //
        // POST: /Page/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            StaticContent staticcontent = db.StaticContents.Single(s => s.ID == id);
            db.StaticContents.DeleteObject(staticcontent);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        private string Change_AV(string ip_str_change)
        {
            Regex v_reg_regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string v_str_FormD = ip_str_change.Normalize(NormalizationForm.FormD);
            char[] BAD_CHARS = new char[] { '!', '@', '#', '$', '%', '_', '"' };
            var result = v_reg_regex.Replace(v_str_FormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
            result = string.Concat(result.Split(BAD_CHARS, StringSplitOptions.RemoveEmptyEntries)).Replace("  ", " ");
            return result.Trim();
        } 

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}